package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.sky.constant.MessageConstant;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.exception.CateGroySQLIntegrityConstraintViolationException;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import com.sky.service.DishService;
import com.sky.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DeadlockLoserDataAccessException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.SQLIntegrityConstraintViolationException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    CategoryMapper categoryMapper;

    @Override
    public Result updateCategory(CategoryDTO categoryDTO) {
        Category category = Category.builder().build();
        BeanUtils.copyProperties(categoryDTO, category);
        categoryMapper.updateCategory(category);

        return Result.success();
    }

    @Override
    public Page<Category> getCategoryPage(CategoryPageQueryDTO categoryPageQueryDTO) {
        return categoryMapper.getCategoryPage(categoryPageQueryDTO);
    }

    @Override
    @Transactional
    public Result updateStatus(Integer status, Long id) {
        Category category = Category.builder().build();
        category.setStatus(status);
        category.setId(id);
        categoryMapper.updateCategory(category);
        return Result.success();
    }

    @Override
    @Transactional
    public Result addCategory(CategoryDTO categoryDTO) {
        Category category = Category.builder().build();
        BeanUtils.copyProperties(categoryDTO, category);
        category.setStatus(1);
        try {
            categoryMapper.addCategory(category);
        } catch (Exception e) {
            throw new CateGroySQLIntegrityConstraintViolationException(MessageConstant.CATEGORYNAMEIS);
        }
        return Result.success();
    }


    @Autowired
    DishService dishService;

    @Autowired
    SetmealService setmealService;

    @Override
    @Transactional
    public Result delCategoryBy(ArrayList<Long> ids) {
        List<Dish> dishs = dishService.findCategoryById(ids);
        if(dishs.size() > 0){
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
        }

        List<Setmeal> setmeals =  setmealService.findCategoryById(ids);
        if(setmeals.size() > 0){
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);
        }

        categoryMapper.delCategoryBy(ids);
        return Result.success();
    }

    @Override
    public Result queryByType(Integer type) {
        List<Category> category = categoryMapper.queryByType(type);
        return Result.success(category);
    }

    @Override
    public List<Category> findDishCateGoryByIds(Long categoryId) {

        return categoryMapper.findDishCateGoryByIds(categoryId);
    }

    @Override
    public Category findDishCateGoryById(Long categoryId) {
        return categoryMapper.findDishCateGoryById(categoryId);
    }

    @Override
    public List<Category> list(Integer type) {
        List<Category> categories = categoryMapper.list(type);
        return categories;
    }
}
